package com.ruoyi.web.controller.market;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.system.domain.SysChannel;
import com.ruoyi.system.domain.SysMarket;
import com.ruoyi.system.domain.SysMember;
import com.ruoyi.system.domain.SysPayload;
import com.ruoyi.system.service.ISysMarketService;
import com.ruoyi.system.service.ISysMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/system/market")
public class SysMarketController extends BaseController {
    @Autowired
    private ISysMarketService iSysMarketService;
    /**
     * 获取app列表
     */
    @PreAuthorize("@ss.hasPermi('system:market:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysMarket app)
    {
        startPage();
        List<SysMarket> list = iSysMarketService.selectMarketList(app);
        return getDataTable(list);
    }
    /**
     * 获取全部app名称
     */
    @PreAuthorize("@ss.hasPermi('system:channel:marketnamelist')")
    @GetMapping("/marketnamelist")
    public List<SysMarket> marketnamelist()
    {
        List<SysMarket> list = iSysMarketService.selectMarketnameList();
        return list;
    }
    @PreAuthorize("@ss.hasPermi('system:market:query')")
    @GetMapping(value = "/{appId}")
    public AjaxResult getInfo(@PathVariable int appId)
    {
        return AjaxResult.success(iSysMarketService.selectMarketById(appId));
    }
    /*添加app*/
    @PreAuthorize("@ss.hasPermi('system:market:add')")
    @Log(title = "app管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysMarket app)
    {
        /*channel.setCreateBy(SecurityUtils.getUsername());*/
        //System.out.println(app.getLogo());
        return toAjax(iSysMarketService.insertMarket(app));
    }
   /* *
     * 修改支付渠道*/

    @PreAuthorize("@ss.hasPermi('system:market:edit')")
    @Log(title = "app管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SysMarket app)
    {
        /*channel.setUpdateBy(SecurityUtils.getUsername());*/
        return toAjax(iSysMarketService.updateMarket(app));
    }
    @PreAuthorize("@ss.hasPermi('system:market:remove')")
    @Log(title = "app管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{appId}")
    public AjaxResult remove(@PathVariable int appId)
    {
        return toAjax(iSysMarketService.deleteMarketById(appId));
    }
    /**
     * LOGO上传
     */
    @Log(title = "LOGO", businessType = BusinessType.UPDATE)
    @PostMapping("/logo")
    public AjaxResult avatar(@RequestParam("logofile") MultipartFile file) throws IOException
    {
        if (!file.isEmpty())
        {
            String logo = FileUploadUtils.upload(RuoYiConfig.getLogoPath(), file);
            AjaxResult ajax = AjaxResult.success();
            ajax.put("imgUrl", logo);
                // 更新缓存用户头像
                //loginUser.getUser().setAvatar(avatar);
//                tokenService.setLoginUser(loginUser);
            return ajax;
        }
        return AjaxResult.error("上传图片异常，请联系管理员");
    }
}
